package org.his.controller.modular.hushi;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.his.dao.CaseInfoDao;
import org.his.dao.PatmentDao;
import org.his.dao.PatmentInfoDao;
import org.his.dao.ReserveDao;
import org.his.pojo.binglidangan.CaseInfo;
import org.his.pojo.caiwu.Patment;
import org.his.pojo.caiwu.PatmentInfo;
import org.his.pojo.huiyuan.MemberCardInfo;
import org.his.pojo.liucheng.Reserve;
import org.his.pojo.quanxuan.User;
import org.his.utils.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/jiuzhen")
@Controller
public class jiuzhenController {

    @Autowired
    private ReserveDao reserveDao;

    @Autowired
    private PatmentDao patmentDao;

    @Autowired
    private PatmentInfoDao patmentInfoDao;

    @Autowired
    private CaseInfoDao caseInfoDao;

    @RequestMapping("/index")
    public String index()throws Exception{
        return "modular/hushi/jiuzhendengji";
    }



    /*查询就诊数据*/
    @RequestMapping("/getlist")
    @ResponseBody
    public Map<String ,Object> getList(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10") Integer limit, Reserve reserve) throws Exception{
        Map<String ,Object> map = new HashMap<>();

        Page<Reserve> pa = PageHelper.startPage(page,limit);
        List<Reserve> reserveList = reserveDao.getjiuzhengList(reserve);

        map.put("code",0);
        map.put("msg",null);
        map.put("count",pa.getTotal());
        map.put("data",reserveList);

        return map;
    }



    @RequestMapping("/jiuzhenfrom")
    public String yuyueForm()throws Exception{
        return "modular/hushi/adddengji";
    }


    /*查询就诊数据*/
    @RequestMapping("/yuyueList")
    @ResponseBody
    public List<Reserve> yuyueList() throws Exception{
        return reserveDao.yuyueList();
    }

    /*查询就诊数据*/
    @RequestMapping("/get")
    @ResponseBody
    public Reserve get(Integer binglihao) throws Exception{
        return reserveDao.get(binglihao);
    }


    /**
     * 添加就诊
     * @throws Exception
     */
    @PostMapping("/addjiuzhen")
    @ResponseBody
    public Map<String,Object> addjiuzhen(Reserve reserve, HttpSession session,Integer yuanid) throws Exception{
        Map<String ,Object> map = new HashMap<>();

        User login_user = (User) session.getAttribute(Common.LOGIN_USER);

        reserve.setIsDiagnosis(1);
        reserve.setOperUserId(login_user);

        if (reserve.getReserveId() != null && reserve.getReserveId() !=0 ){
            reserveDao.update(reserve);
        }else{
            reserveDao.add(reserve);
        }



        //修改预约类型
        reserveDao.updReserveType(yuanid);

        //财务添加信息
        Patment patment = new Patment();
        patment.setMemberCaseNo(reserve.getMedicalRecordNo());
        MemberCardInfo memberCardInfo = new MemberCardInfo();
        memberCardInfo.setCardMemberId(Integer.parseInt(reserve.getMembershipCardNo()));
        patment.setMemberId(memberCardInfo);
        patment.setMemberCaseName(reserve.getReserveName());
        patmentDao.add(patment);

        //财务详细表
        PatmentInfo patmentInfo = new PatmentInfo();
        patmentInfo.setPayId(patment.getPayId());
        patmentInfo.setOption("挂号费");
        patmentInfo.setNumber(1);
        patmentInfo.setMoney(reserve.getRegistryFee());

        patmentInfoDao.add(patmentInfo);


        User user = new User();
        user.setUserid(reserve.getUserId().intValue());

        //档案表
        CaseInfo caseInfo = new CaseInfo();
        caseInfo.setMemberCaseNo(reserve);
        caseInfo.setMemberId(memberCardInfo);
        caseInfo.setUserId(user);
        caseInfo.setCaseStatus(0);
        caseInfo.setCaseTell(reserve.getMemo());
        caseInfo.setStateData(reserve.getReserveopentime());
        caseInfo.setEndData(reserve.getReserveopencole());


        caseInfoDao.add(caseInfo);


        return map;
    }


}
